package org.csu.secondhand.api.service.impl;

import org.csu.secondhand.api.entity.Account;
import org.csu.secondhand.api.persistence.AccountMapper;
import org.csu.secondhand.api.service.AccountService;
import org.csu.secondhand.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;


    @Override
    public Account login(String code) {
        String openId = HttpUtil.getOpenId(code);
        Account account = accountMapper.selectById(openId);
        if (account == null) {
            account = new Account(openId, "", 0, "", "", "", "");
            accountMapper.insert(account);
        }
        return account;
    }

    @Override
    public Account editAccount(Account account) {
        accountMapper.updateById(account);
        return account;
    }

    @Override
    public Account getAccount(String id) {
        Account account = accountMapper.selectById(id);
        return account;
    }
}
